/*
 * Copyright 2008 p.langer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.ac.tuwien.big.easyholdem.player;

import java.util.Collection;
import java.util.HashSet;

/**
 * <p>
 * Implements the {@link PlayerDAO} with some {@link Player} objects.
 * </p>
 * 
 * <p>
 * This class is just a mock and not suitable for real usage. The player objects
 * are newly created at each instantiation of this class and are saved in
 * memory.
 * </p>
 * 
 * <p>
 * Created on 17.01.2008 at 15:19:30.
 * </p>
 * 
 * @author Philip Langer <langer[at]big[dot]tuwien[dot]ac[dot]at>
 */
public class MemoryPlayerDAO implements PlayerDAO {
	
	private long lastId = 0;
	private Collection<Player> players = new HashSet<Player>();
	
	public MemoryPlayerDAO() {
	}
	
	private Collection<Player> createTestObjects() {
		Collection<Player> players = new HashSet<Player>();
		
		Player p1 = new Player();
		p1.setId(++lastId);
		p1.setFirstName("Phil");
		p1.setLastName("Hellmuth");
		p1.setUserName("phil");
		p1.setStack(1000);
		players.add(p1);
		
		Player p2 = new Player();
		p2.setId(++lastId);
		p2.setFirstName("Barbara");
		p2.setLastName("Enright");
		p2.setUserName("barbara");
		p2.setStack(1100);
		players.add(p2);
		
		Player p3 = new Player();
		p3.setId(++lastId);
		p3.setFirstName("Daniel");
		p3.setLastName("Negreanu");
		p3.setUserName("daniel");
		p3.setStack(1150);
		players.add(p3);
		
		return players;
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#delete(at.ac.tuwien.big.easyholdem.player.Player)
	 */
	public void delete(Player player) {
		this.players.remove(player);
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#getAll()
	 */
	public Collection<Player> getAll() {
		return this.players;
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#getPlayerById(long)
	 */
	public Player getPlayerById(long id) {
		for (Player p : players) {
			if (p.getId() == id) return p;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#getPlayerByUsername(java.lang.String)
	 */
	public Player getPlayerByUsername(String userName) {
		for (Player p : players) {
			if (p.getUserName().equals(userName)) return p;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#persist(at.ac.tuwien.big.easyholdem.player.Player)
	 */
	public Player persist(Player player) {
		player.setId(++lastId);
		players.add(player);
		return player;
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#update(at.ac.tuwien.big.easyholdem.player.Player)
	 */
	public void update(Player player) {
		if (player.getId() < 1 || !players.contains(player)) {
			throw new IllegalArgumentException("player " + player.getUserName() + " is not persistent.");
		}
		this.players.remove(getPlayerById(player.getId()));
		this.players.add(player);
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#initialize()
	 */
	public void initialize() {
		players = createTestObjects();
	}

	/*
	 * (non-Javadoc)
	 * @see at.ac.tuwien.big.easyholdem.player.PlayerDAO#isInitialized()
	 */
	public boolean isInitialized() {
		return true;
	}

	
	
}
